package com.light.component;

import com.light.enums.Level;
import com.light.exception.H2Exception;
import com.light.layout.ModalDialog;
import com.light.model.GitAuthInfo;
import com.light.util.H2PoolUtils;
import com.light.util.JGitUtils;
import com.light.util.NoticeUtils;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import org.apache.commons.lang3.StringUtils;

public class AuthenticationPane extends ModalDialog {

    private AuthHandler handler;
    private static AuthenticationPane authPane;
    private final TextField usernameField = new TextField();
    private final PasswordField passwordField = new PasswordField();
    private final Button saveButton = new Button("确定");

    public static AuthenticationPane getInstance() {
        if (authPane == null) {
            authPane = new AuthenticationPane();
        }
        return authPane;
    }

    public AuthenticationPane() {
        super();
        usernameField.setPromptText("手机/邮箱/用户名");
        usernameField.setMaxWidth(300);
        passwordField.setPromptText("请输入密码");
        passwordField.setMaxWidth(300);
        saveButton.setMaxWidth(300);
        var root = new VBox(10, usernameField, passwordField, saveButton);
        root.setPadding(new Insets(20));
        header.setTitle(null);
        content.setBody(root);
        content.setFooter(null);

        initEvent();
    }

    private void initEvent() {
        saveButton.setOnMouseClicked(event -> {
            String username = usernameField.getText();
            String password = passwordField.getText();
            if (StringUtils.isAnyBlank(username, password)) {
                NoticeUtils.show(null, "用户名密码不能为空", Level.WARN);
                return;
            }
            // 处理公共方法
            try {
                handler.handler(username, password);
            } catch (Exception e) {
                NoticeUtils.show(null, "更新权限异常", Level.DANGER);
            }
            // 关闭界面
            close();
            event.consume();
        });
    }

    public GitAuthInfo dealAuthInfo(String username, String password, String repoType) {
        GitAuthInfo authInfo = new GitAuthInfo(0, repoType, username, password, "");
        // 入库
        try {
            GitAuthInfo existsAuthInfo = H2PoolUtils.queryAuthInfo(repoType);
            if (existsAuthInfo != null) {
                // 克隆或者下载时权限异常会执行这段代码
                H2PoolUtils.updateAuthInfo(repoType, username, password);
            } else {
                H2PoolUtils.insertAuthInfo(authInfo);
            }
            JGitUtils.AUTH_INFO_MAP.put(repoType, authInfo);
        } catch (H2Exception e) {
            NoticeUtils.show(null, e.getMessage(), Level.WARN);
            return null;
        }
        return authInfo;
    }

    public void refreshData(String remoteUrl, AuthHandler handler) {
        this.handler = handler;
        header.setTitle(JGitUtils.getType(remoteUrl));
    }
}
